﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;

namespace sWarranty.WEB
{
    public partial class UserRegister : System.Web.UI.Page
    {
        public int _currentUserID;
        public string _currentUserEmpNo = "";
        public string _currentUserName;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["UrltoFw"] = "UserRegister.aspx";
            string updateValuesScript =
                @"
                    function updateValues(popupValues) {
                        //email|EMPNO|NAME|DEPT
                        var ReturnValue = popupValues[0];
                        var lines = (ReturnValue).split('|');
                        var email = '';
                        var empNo = '';
                        var fullName = '';
                        var dept = '';
                        $.each(lines, function (key, line) {
                            email = lines[0];
                            empNo = lines[1];
                            fullName = lines[2];
                            dept = lines[3];
                        });
                        document.getElementById('txtAccount').value = email;
                        $('#lblEmpNo').text(empNo);
                        $('#lblFullName').text(fullName);
                        $('#lblDept').text(dept);
                        
                        document.getElementById('txtAccount2').value = email;
                        document.getElementById('txtEmpNo2').value = empNo;
                        document.getElementById('txtFullName2').value = fullName;
                        document.getElementById('txtDept2').value = dept;
                    }
                 ";
            this.ClientScript.RegisterStartupScript(Page.GetType(), Guid.NewGuid().ToString(), updateValuesScript.ToString(), true);
            btnSelect.Attributes.Add("onclick", "openPopUp('FindUser.aspx','open')");

            _currentUserID = GetUserLoginID();
            if (_currentUserID == 0)
                Response.Redirect("login.aspx");

            if (!IsPostBack)
                LoadAuthority();


        }
        protected int GetUserLoginID()
        {
            int t = 0;
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    _currentUserName = u.Single().ACCOUNT;
                    _currentUserEmpNo = u.Single().EMPNO;
                    return u.Single().ID;
                }
            }
            catch (Exception)
            {
                t = 0;
            }
            return t;
        }

        protected void LoadAuthority()
        {
            Entities db = new Entities();
            var au = from t in db.TB_LEVEL
                     select t;
            foreach (var item in au)
            {
                ListItem i = new ListItem();
                i.Value = item.ID.ToString();
                i.Text = item.NAME + " - " + item.REMARK;
                dropAuthority.Items.Add(i);
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtAccount2 .Text))
                {
                    ShowWarning("You must select user by click '...' button");
                    return;
                }
                if (string.IsNullOrEmpty(txtPass.Text))
                {
                    ShowWarning("You must input Password");
                    return;
                }
                if (txtPass.Text != txtConfirm.Text)
                {
                    ShowWarning("Password not same");
                    return;
                }

                Entities db = new Entities();
                var exists = from t in db.TB_USER
                             where t.ACCOUNT == txtAccount2 .Text
                             select t;
                if (exists.Count() > 0)
                {
                    ShowError("User exists");
                    return;
                }
                var _max = (from t in db.TB_USER select t.ID).Max();
                TB_USER u = new TB_USER();
                u.ID = _max + 1;
                u.EMPNO = txtEmpNo2.Text;
                u.ACCOUNT = txtAccount2.Text;
                u.DISPLAY_NAME = txtFullName2.Text;
                u.PASS = EncDec.Encrypt(txtPass.Text.Trim(), "");
                u.ENTRY_USER_ID = _currentUserID;
                u.ENTRY_USER_NAME = _currentUserName;
                u.ENTRY_DATE_TIME = DateTime.Now;
                u.REGION_ID = dropUserRegion.SelectedValue;
                u.ACTIVE = 1;

                db.TB_USER.AddObject(u);
                db.SaveChanges();//fai save, neu ko save dc, thi loi luon, se ko add user level nua;

                //add authority level
                TB_USER_LEVEL l = new TB_USER_LEVEL();
                var _maxUserLevel = (from t in db.TB_USER_LEVEL select t.ID).Max();
                l.ID = _maxUserLevel + 1;
                l.USER_ID = _max + 1;
                l.LEVEL_ID = Convert.ToInt32(dropAuthority.SelectedValue);
                l.ENTRY_USER_ID = _currentUserID;
                l.ENTRY_USER_NAME = _currentUserName;
                l.ENTRY_DATE_TIME = DateTime.Now;

                db.TB_USER_LEVEL.AddObject(l);
                db.SaveChanges();

                //log
                if (DBAction.LogUser == true)
                {
                    TB_CM_SYS_LOG lg = new TB_CM_SYS_LOG();
                    lg.ID = String.Format("{0:u}", DateTime.Now);
                    lg.ACTION1 = _currentUserName + " Create New User: " + txtAccount2.Text;
                    lg.ENTRY_USER_ID = _currentUserEmpNo;
                    lg.ENTRY_DATE_TIME = DateTime.Now;
                    lg.TITLE = _currentUserName + " Create New User: " + txtAccount2.Text;
                    db.TB_CM_SYS_LOG.AddObject(lg);
                    db.SaveChanges();
                }
            }
            catch (Exception ee)
            {
                ShowError(ee);
            }
        }


        public void ShowError(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification error\"><div class=\"text\"><p><strong>" + er + " </strong> </p></div></div>", false);
        }
        public void ShowError(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification error\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowInfo(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification info\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
        }
        public void ShowInfo(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification info\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowWarning(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification warning\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
        }
        public void ShowWarning(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification warning\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowSuccess(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification success\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
        }
        public void ShowSuccess(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification success\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowTip(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification tip\"><div class=\"text\"><p><strong> " + er + "</strong> </p></div></div>", false);
        }
        public void ShowTip(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification tip\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
    }
}